// models/taskModel.js - 任务数据模型
class TaskModel {
  constructor() {
    this.tasks = []
    this.currentId = 1
  }

  // 创建新任务
  createTask(taskData) {
    const task = {
      id: `task_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`,
      title: taskData.title || '',
      description: taskData.description || '',
      level: taskData.level || 1,
      parentId: taskData.parentId || null,
      priority: taskData.priority || 2,
      completed: false,
      startTime: null,
      endTime: null,
      duration: null,
      createTime: new Date(),
      updateTime: new Date(),
      children: []
    }

    this.tasks.push(task)
    return task
  }

  // 获取任务列表
  getTasks(filter = {}) {
    let tasks = this.tasks

    // 按层级过滤
    if (filter.level !== undefined) {
      tasks = tasks.filter(task => task.level === filter.level)
    }

    // 按完成状态过滤
    if (filter.completed !== undefined) {
      tasks = tasks.filter(task => task.completed === filter.completed)
    }

    // 按日期过滤
    if (filter.date) {
      tasks = tasks.filter(task => {
        const taskDate = new Date(task.createTime).toDateString()
        const filterDate = new Date(filter.date).toDateString()
        return taskDate === filterDate
      })
    }

    return tasks
  }

  // 获取任务详情
  getTaskById(taskId) {
    return this.tasks.find(task => task.id === taskId)
  }

  // 更新任务
  updateTask(taskId, updateData) {
    const taskIndex = this.tasks.findIndex(task => task.id === taskId)
    if (taskIndex !== -1) {
      this.tasks[taskIndex] = {
        ...this.tasks[taskIndex],
        ...updateData,
        updateTime: new Date()
      }
      return this.tasks[taskIndex]
    }
    return null
  }

  // 删除任务
  deleteTask(taskId) {
    const taskIndex = this.tasks.findIndex(task => task.id === taskId)
    if (taskIndex !== -1) {
      this.tasks.splice(taskIndex, 1)
      return true
    }
    return false
  }

  // 添加子任务
  addSubtask(parentId, subtaskData) {
    const parentTask = this.getTaskById(parentId)
    if (parentTask && parentTask.level < 3) {
      const subtask = this.createTask({
        ...subtaskData,
        level: parentTask.level + 1,
        parentId: parentId
      })
      
      if (!parentTask.children) {
        parentTask.children = []
      }
      parentTask.children.push(subtask)
      return subtask
    }
    return null
  }

  // 获取子任务
  getSubtasks(parentId) {
    const parentTask = this.getTaskById(parentId)
    return parentTask ? parentTask.children || [] : []
  }

  // 开始计时
  startTimer(taskId) {
    const task = this.getTaskById(taskId)
    if (task && !task.completed) {
      task.startTime = new Date()
      task.endTime = null
      task.duration = null
      return task
    }
    return null
  }

  // 结束计时
  stopTimer(taskId) {
    const task = this.getTaskById(taskId)
    if (task && task.startTime && !task.endTime) {
      task.endTime = new Date()
      task.duration = Math.floor((task.endTime - task.startTime) / (1000 * 60)) // 分钟
      return task
    }
    return null
  }

  // 标记任务完成
  completeTask(taskId) {
    const task = this.getTaskById(taskId)
    if (task) {
      task.completed = true
      if (task.startTime && !task.endTime) {
        this.stopTimer(taskId)
      }
      return task
    }
    return null
  }

  // 获取任务统计
  getTaskStats(date = null) {
    let tasks = this.tasks

    if (date) {
      tasks = tasks.filter(task => {
        const taskDate = new Date(task.createTime).toDateString()
        const filterDate = new Date(date).toDateString()
        return taskDate === filterDate
      })
    }

    const stats = {
      total: tasks.length,
      completed: tasks.filter(task => task.completed).length,
      totalTime: tasks.reduce((sum, task) => sum + (task.duration || 0), 0),
      level1: tasks.filter(task => task.level === 1).length,
      level2: tasks.filter(task => task.level === 2).length,
      level3: tasks.filter(task => task.level === 3).length
    }

    const completedTasks = tasks.filter(task => task.completed && task.duration)
    stats.avgTime = completedTasks.length > 0 ? 
      Math.round(stats.totalTime / completedTasks.length) : 0
    stats.completionRate = stats.total > 0 ? 
      Math.round((stats.completed / stats.total) * 100) : 0

    return stats
  }
}

module.exports = TaskModel